package com.ustarlink.keyframeanimation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = (ImageView) findViewById(R.id.imageView);

        // Create the AnimationDrawable in which we will store all frames of the animation
        final AnimationDrawable animationDrawable = new AnimationDrawable();

        for (int i = 0; i < 10; i++){
            animationDrawable.addFrame(getDrawableForFrameNumber(i), 300);
        }
        // Run until we say stop
        animationDrawable.setOneShot(false);

        imageView.setImageDrawable(animationDrawable);

        // When the user clicks on the image, toggle the animation on/off
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (animationDrawable.isRunning()){
                    animationDrawable.stop();
                } else {
                    animationDrawable.start();
                }
            }
        });
    }
    // the 'frame' in the app are nothing more than a gray background with text indicating
    // the number of the frame
    private Drawable getDrawableForFrameNumber(int frameNumber) {
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.GRAY);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(80);
        paint.setColor(Color.BLACK);
        canvas.drawText("Frame " + frameNumber, 40, 220, paint);
        return new BitmapDrawable(getResources(), bitmap);

    }
}
